﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Product : System.Web.UI.Page
{
    DataTable dtb = new DataTable();
    string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.sql = "Select TenXe,NgoaiThat,NamSX,Image,MaXe from ThongTinCoBan";
            this.Display(sql);
        }
        if (Request["Tenhang"] != null)
        {
            string request = Request["Tenhang"];
            this.sql = string.Format(@"Select TenXe,NgoaiThat,NamSX,Image,MaXe From ThongTinCoBan as a, hangxe as b WHERE b.TenHang='{0}' and a.MaHangXe = b.MaHangXe", request);
            this.Display(sql);
        }
    }
    protected void Display(string sql)
    {
        
        dtb = DataBase.GetDataTable(sql);
        DataList1.DataSource = dtb;
        DataList1.DataBind();
        PhanTrang();
    }
    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtn = (ImageButton)sender;

        Response.Redirect("ProductDetail.aspx?Product=" + ibtn.CommandArgument);

        if (Request["Tenhang"] != null)
        {
            string req = Request["Tenhang"];
            Response.Redirect("ProductDetail.aspx?Tenhang="+req+"&Product=" + ibtn.CommandArgument);
        }

    }
    void PhanTrang()
    {

        CollectionPager1.DataSource = dtb.DefaultView;
        CollectionPager1.PageSize = 12;
        CollectionPager1.BindToControl = DataList1;
        DataList1.DataSource = CollectionPager1.DataSourcePaged;

    }

    protected void btnCartAdd_Click(object sender, ImageClickEventArgs e)
    {
        ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"];

        ImageButton btn = (ImageButton)sender;
        string maxe = btn.CommandArgument;
        cart.Add(maxe, 1);

        MasterPage master = (MasterPage)this.Master;
        master.RefreshShoppingCartInfo();
    }
}
